package com.smart.lab.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.smart.lab.R;
import com.smart.lab.model.CropPestDiseaseEntity;

import java.util.ArrayList;
import java.util.List;

/**
 *  美丽乡村列表
 */
public class ItemListAdapterOfPest extends RecyclerView.Adapter<ItemListAdapterOfPest.ViewHolder> {

    public List<CropPestDiseaseEntity> _datas=new ArrayList<>();
    private LayoutInflater mInflater;
    Context _context;
    private OnItemClickListener _onItemClickListener;

    int screenWidth;
    int screenHeight;

    public ItemListAdapterOfPest(List<CropPestDiseaseEntity> datas, Context context) {
        _datas = datas;
        _context = context;
        WindowManager wm = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        screenWidth = wm.getDefaultDisplay().getWidth();
        screenHeight = wm.getDefaultDisplay().getHeight();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View v = LayoutInflater.from(_context).inflate(R.layout.item_beautiful_valliage, parent, false);

        // 实例化viewholder
        ViewHolder viewHolder = new ViewHolder(v);

        ViewGroup.LayoutParams lp = viewHolder.iv.getLayoutParams();
        lp.width = screenWidth/2;
        lp.height = screenWidth/2;
        viewHolder.iv.setLayoutParams(lp);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        CropPestDiseaseEntity baseProjectEntity =(CropPestDiseaseEntity) _datas.get(position);

//        if (baseProjectEntity.getImages()!=null && baseProjectEntity.getImages().size()>0) {
//            Glide.with(_context).load(baseProjectEntity.getImages().get(0).getUrl()).into(holder.iv);
//        }else {
//            holder.iv.setImageResource(R.mipmap.logo);
//        }
        holder.tvName.setText(baseProjectEntity.getName());


    }

    @Override
    public int getItemCount() {
        return _datas == null ? 0 : _datas.size();
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this._onItemClickListener = listener;
    }


    public void refreshData(List<CropPestDiseaseEntity> list) {
//        _datas = list; //不起作用
        _datas = new ArrayList<CropPestDiseaseEntity>();
        _datas.addAll(list);
        notifyDataSetChanged();
    }


    public void addData(List<CropPestDiseaseEntity> datas) {
        _datas.addAll(datas);
        notifyDataSetChanged();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView iv;
        TextView tvName;


        public ViewHolder(View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.iv);
            tvName = itemView.findViewById(R.id.tvName);


            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (_onItemClickListener != null) {
                        _onItemClickListener.onItemClick(v, getLayoutPosition(), _datas.get(getLayoutPosition()));
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position, CropPestDiseaseEntity baseProjectEntity);
    }
}
